package com.cuijq.ms.singleton.generator;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Classname BackendServer
 * @Description 多利模式
 * @Date 2021/5/5 15:24
 * @Created by CuiJQ
 */
public class BackendServer {

  private long serverNo;
  private String serverAddress;
  private static final int SERVER_COUNT = 3;
  private static final Map<Long,BackendServer> serverInstances = new HashMap<>();

  static {
    serverInstances.put(1L, new BackendServer(1L, "192.134.22.138:8080"));
    serverInstances.put(2L, new BackendServer(2L, "192.134.22.139:8080"));
    serverInstances.put(3L, new BackendServer(3L, "192.134.22.140:8080"));
  }

  private BackendServer(long serverNo, String serverAddress) {
    this.serverNo = serverNo;
    this.serverAddress = serverAddress;
  }

  public BackendServer getInstance(long serverNo) {
    return serverInstances.get(serverNo);
  }

  public BackendServer getRandomInstance() {
    Random r = new Random();
    int no = r.nextInt(SERVER_COUNT) + 1;
    return serverInstances.get(no);
  }
}
